﻿using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ContractManagementSystem.BusinessLogicLayer;
using ContractManagementSystem.EntityObjects;

namespace ContractManagementSystem.DataAccessLayer
{
    public static class ContractDao
    {
        // Get list of active contracts
        public static DataTable ListActiveContracts()
        {
            var dataTable = CommonDao.GetDataTable("[spGetContractList] @status = 1");
            return dataTable;
        }

        //number


        // Find contracts
        public static DataTable FindContract(ContractEntity contractEntity, string[] operationArrays,  int findingOptions)
        {
            /* operationArrays[0] = operation for field Created date
             * operationArrays[1] = operation for field Start date
             * operationArrays[2] = operation for field End date
             * operationArrays[3] = operation for field Contract value
             */
            var sqlFind = "[spFindContracts] @CreatedDate = '" + contractEntity.CreatedDate +
                          "', @OperationCreatedDate = '" + operationArrays[0] + "'," +
                          "@StartDate = '" + contractEntity.StartDate + "', @OperationStartDate = '" +
                          operationArrays[1] + "', " +
                          "@EndDate = '" + contractEntity.EndDate + "', @OperationEndDate = '" + operationArrays[2] +
                          "', " +
                          "@ContractNumber = '" + contractEntity.ContractNumber + "', @ContractTitle = N'" +
                          contractEntity.ContractTitle + "', " +
                          "@ContractValue = '" + CommonLogic.ConvertToString(contractEntity.ContractValue) +
                          "', @OperationValue = '" + operationArrays[3] + "', " +
                          "@Status = '" + CommonLogic.ConvertToString(contractEntity.Status) + "', @Contractor = N'" + contractEntity.ContractorName +
                          "', " +
                          "@Client = N'" + contractEntity.ClientName + "', @Consultant = N'" +
                          contractEntity.ConsultantName + "', " +
                          "@Description = N'" + contractEntity.Description + "', @findingOptions = '" + CommonLogic.ConvertToString(findingOptions) +
                          "'";

            return CommonDao.GetDataTable(sqlFind);
        }

        // add by chinhbv
        public static DataTable ListActiveContractsByContractor(string userName)
        {
            var dataTable =
                CommonDao.GetDataTable("sp_Contract_GetContractListByContractor @UserName='" + userName + "', @Status=1");
            return dataTable;
        }

        public static DataTable ListActiveContractsByConsultant(string user)
        {
            var dataTable =
                CommonDao.GetDataTable("sp_Contract_GetContractListByConsultant @UserName='" + user + "', @Status=1");
            return dataTable;
        }
        /// <summary>
        /// Search page report contract by admin
        /// </summary>
        /// <param name="contractEntity"></param>
        /// <returns></returns>
        public static DataTable ReportSearchContract(ContractEntity contractEntity)
        {
            var query = "sp_Contract_ReportSearchContract @ContractNumber='" + contractEntity.ContractNumber + "',"
                        + "@Status='"+contractEntity.Status+"'";
            var result = CommonDao.GetDataTable(query);
            return result;
        }
        /// <summary>
        /// Search page report contract by consultant
        /// </summary>
        /// <param name="user"></param>
        /// <param name="contractEntity"></param>
        /// <returns></returns>
        public static DataTable ReportSearchContractByConsultant(string user, ContractEntity contractEntity)
        {
            var query = "sp_Contract_ReportSearchContractByConsultant @UserName='"+user+"',"
            +"@ContractNumber='" + contractEntity.ContractNumber + "',"
                        + "@CreatedDate='" + contractEntity.CreatedDate + "',"
                        + "@StartDate='" + contractEntity.StartDate + "',"
                        + "@Status='" + contractEntity.Status + "'";
            var result = CommonDao.GetDataTable(query);
            return result;
        }
    }
}
